package name.vaccari.matteo.tai.chat.persistence;

import java.util.List;

import name.vaccari.matteo.tai.chat.domain.Message;
import name.vaccari.matteo.tai.chat.domain.MessageRepository;

public class DatabaseMessageRepository implements MessageRepository {

	private final DatabaseExecutor executor;

	public DatabaseMessageRepository(DatabaseExecutor connector) {
		this.executor = connector;
	}

	public List<Message> getLatestMessages(int howMany) {
		String sql = "select * from messages order by id desc limit ?";
		return executor.select(sql, new MessageMapper(), howMany);
	}

	public void save(Message message) {
		executor.execute("insert into messages (text, created_at) values (?, now())", message.getText());
	}

	public void addMessage(String message) {
		throw new RuntimeException("Not yet implemented!");
	}
	
	

}
